import socket

def send_udp_data(target_ip, target_port, message):
    # 创建 UDP 套接字
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # 发送数据
        sock.sendto(message.encode(), (target_ip, target_port))
        print(f"成功发送数据到 {target_ip}:{target_port}")
    except Exception as e:
        print(f"发送数据时出现错误: {e}")
    finally:
        # 关闭套接字
        sock.close()
if __name__ == "__main__":
    # 目标 IP 地址
    target_ip = "127.0.0.1"
    # 目标端口
    target_port = 50789

    print("开始循环发送数据，请输入要发送的消息（输入 'quit' 退出）：")
    while True:
        # 获取用户输入
        message = input()
        if message.lower() == 'quit':
            break
        # 发送用户输入的消息
        send_udp_data(target_ip, target_port, message)
